﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using TMDT.BusinessObjects;

namespace TMDT.BusinessLogic
{
    public class LoaiSanPhamManager
    {
        public bool Add(LoaiSanPham entity)
        {
            return BusinessHelper.CapNhatDuLieu(new Command
                                                    {
                                                        CommandText = "spLoaiSanPhamInsert",
                                                        CommandType = CommandType.StoredProcedure,
                                                        Parameter = new[]
                                                                        {
                                                                            new SqlParameter("@IDLoaiSanPham",
                                                                                             entity.IDLoaiSanPham),
                                                                            new SqlParameter("@TenLoaiSanPham",
                                                                                             entity.TenLoaiSanPham),
                                                                            new SqlParameter("@IDChungLoai",
                                                                                             entity.IDChungLoai)
                                                                        }
                                                    });
        }

        public bool Update(LoaiSanPham entity)
        {
            return BusinessHelper.CapNhatDuLieu(new Command
                                                    {
                                                        CommandText = "spLoaiSanPhamUpdate",
                                                        CommandType = CommandType.StoredProcedure,
                                                        Parameter = new[]
                                                                        {
                                                                            new SqlParameter("@IDLoaiSanPham",
                                                                                             entity.IDLoaiSanPham),
                                                                            new SqlParameter("@TenLoaiSanPham",
                                                                                             entity.TenLoaiSanPham),
                                                                            new SqlParameter("@IDChungLoai",
                                                                                             entity.IDChungLoai)
                                                                        }
                                                    });
        }

        public bool Remove(object id)
        {
            return BusinessHelper.CapNhatDuLieu(new Command
                                                    {
                                                        CommandText = "spLoaiSanPhamDelete",
                                                        CommandType = CommandType.StoredProcedure,
                                                        Parameter = new[]
                                                                        {
                                                                            new SqlParameter("@IDLoaiSanPham", id)
                                                                        }
                                                    });
        }

        public List<LoaiSanPham> GetLoaiSanPham()
        {
            List<LoaiSanPham> loaiSanPham = new List<LoaiSanPham>();
            DataSet dtSet = new DataSet();
            dtSet = BusinessHelper.TruyVanDuLieu(new Command
                                                     {
                                                         CommandText = "select * from LoaiSanPham",
                                                         CommandType = CommandType.Text,
                                                     });
            foreach (DataRow row in dtSet.Tables[0].Rows)
            {
                loaiSanPham.Add(LoaiSanPham.Convert(row));
            }
            return loaiSanPham;
        }

        public LoaiSanPham GetLoaiSanPham(object id)
        {
            DataSet dtSet = new DataSet();
            dtSet = BusinessHelper.TruyVanDuLieu(new Command
                                                     {
                                                         CommandText =
                                                             "select * from LoaiSanPham where IDLoaiSanPham = @IDLoaiSanPham",
                                                         CommandType = CommandType.Text,
                                                         Parameter = new[]
                                                                         {
                                                                             new SqlParameter("@IDLoaiSanPham", id),
                                                                         }
                                                     });
            return LoaiSanPham.Convert(dtSet.Tables[0].Rows[0]);
        }

        public List<LoaiSanPham> GetLoaiSanPhamByChungLoai(object idChungLoai)
        {
            List<LoaiSanPham> loaiSanPham = new List<LoaiSanPham>();
            DataSet dtSet = new DataSet();
            dtSet = BusinessHelper.TruyVanDuLieu(new Command
                                                     {
                                                         CommandText =
                                                             "select * from LoaiSanPham where IDChungLoai = @IDChungLoai",
                                                         CommandType = CommandType.Text,
                                                         Parameter = new[]
                                                                         {
                                                                             new SqlParameter("@IDChungLoai",
                                                                                              idChungLoai),
                                                                         }
                                                     });
            foreach (DataRow row in dtSet.Tables[0].Rows)
            {
                loaiSanPham.Add(LoaiSanPham.Convert(row));
            }
            return loaiSanPham;
        }
    }
}
